package com.lilicould.service.impl;

import com.lilicould.dao.ContractDao;
import com.lilicould.dao.HouseDao;
import com.lilicould.entity.*;
import com.lilicould.service.ContractService;
import com.lilicould.util.MyBatisUtil;
import org.apache.ibatis.session.SqlSession;

import java.util.ArrayList;

public class ContractServiceImpl implements ContractService {
    // 添加合同
    @Override
    public boolean addContract(LeaseContract leaseContract, ContractApartment contractApartment, ContractTenant contractTenant) {
        SqlSession sqlSession = MyBatisUtil.getSqlSession();
        try {
            sqlSession.getConnection().setAutoCommit(false);
            ContractDao contractDao = sqlSession.getMapper(ContractDao.class);
            // 自动创建leaseId
            String leaseId = "CT"+System.currentTimeMillis();
            leaseContract.setContractId(leaseId);
            contractApartment.setContractId(leaseId);
            contractTenant.setContractId(leaseId);

            int leaseResult = contractDao.addContract(leaseContract);
            int apartmentResult = contractDao.addContractApartment(contractApartment);
            int tenantResult = contractDao.addContractTenant(contractTenant);

            if (leaseResult > 0 && apartmentResult > 0 && tenantResult > 0) {
                sqlSession.commit();
                return true;
            }else {
                sqlSession.rollback();
                return false;
            }
        }catch (Exception e) {
            System.out.println(e.getMessage());
            sqlSession.rollback();
            return false;
        }finally {
            sqlSession.close();
        }
    }

    // 查询所有合同详情
    @Override
    public ArrayList<ContractDetails> selectAllContractDetails() {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            ContractDao contractDao = sqlSession.getMapper(ContractDao.class);
            // 得到所有合同
            ArrayList<LeaseContract> leaseContracts = contractDao.selectAllContracts();

            // 创建所有合同详情
            ArrayList<ContractDetails> contractDetails = new ArrayList<>();
            // 遍历所有合同
            for (LeaseContract leaseContract : leaseContracts) {
                Apartment apartment = contractDao.selectApartmentByContractId(leaseContract.getContractId());
                ArrayList<Tenant> tenants = contractDao.selectTenantsByContractId(leaseContract.getContractId());
                contractDetails.add(new ContractDetails(leaseContract, apartment, tenants));
            }
            return contractDetails;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }


    // 判断该员工是否是该楼栋的负责人
    @Override
    public boolean isYours(String employeeId, String buildingId) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            HouseDao houseDao = sqlSession.getMapper(HouseDao.class);
            Building building = houseDao.selectBuildingByBuildingId(buildingId);
            return building.getManagerId().equals(employeeId);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return false;
        }
    }

    // 判断该公寓是否被出租
    @Override
    public String isLeased(String buildingId, String apartmentNo) {
        try {
            SqlSession sqlSession = MyBatisUtil.getSqlSession();
            ContractDao contractDao = sqlSession.getMapper(ContractDao.class);
            String contractId = contractDao.selectContractIdByBuildingIdAndApartmentNo(buildingId, apartmentNo);
            return contractId;
        }catch (Exception e){
            System.out.println(e.getMessage());
            return null;
        }
    }

    @Override
    // 添加合同和租户
    public boolean addContractTenant(ContractTenant contractTenant) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            ContractDao contractDao = sqlSession.getMapper(ContractDao.class);
            int result = contractDao.addContractTenant(contractTenant);
            return result > 0;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    // 通过合同id删除合同
    @Override
    public boolean deleteContract(String contractId) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            ContractDao contractDao = sqlSession.getMapper(ContractDao.class);
            int result = contractDao.deleteContract(contractId);
            return result > 0;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }

    // 更新合同信息
    @Override
    public boolean updateContract(LeaseContract leaseContract) {
        try (SqlSession sqlSession = MyBatisUtil.getSqlSession()) {
            ContractDao contractDao = sqlSession.getMapper(ContractDao.class);
            int result = contractDao.updateContract(leaseContract);
            return result > 0;
        } catch (Exception e) {
            System.out.println(e.getMessage());
            return false;
        }
    }
}
